<template>
	<view class="edit_box">
		<bottom-rect type="normal"></bottom-rect>
		<my-from ref="myForm" class="form" btnText="更新"></my-from>
	</view>
</template>

<script>
	import bottomRect from '@/components/bottomRect/bottomRect.vue'
	import myFrom from '@/components/myForm/myForm.vue'

	import {
		mapState,
		mapMutations
	} from 'vuex'

	export default {
		name: 'edit',
		options: {
			styleIsolation: 'shared',
		},
		components: {
			bottomRect,
			myFrom
		},
		data() {
			return {
				// 需要编辑的数据信息
				formData: {}
			}
		},
		computed: {
			// 映射状态管理的state
			...mapState({
				// 当前第一大类的选择状态
				first_category_index: state => state.first_category_index,
				// 当前第二大类的选择状态
				second_category_index: state => state.second_category_index
			})
		},
		methods: {
			// 从vuex映射到此页面方法中
			...mapMutations(['edit_first_index', 'edit_second_index', 'edit_second_value']),
			// 判断是什么类型的信息
			estimate_type() {
				return new Promise(resolve => {
					if (this.formData.tags == 'lost') {
						this.edit_first_index(0)
						if (this.formData.type == '校园卡') {
							this.edit_second_index(0)
							this.edit_second_value('校园卡')
						} else if (this.formData.type == '身份证') {
							this.edit_second_index(1)
							this.edit_second_value('身份证')
						} else if (this.formData.type == '电影卡') {
							this.edit_second_index(2)
							this.edit_second_value('电影卡')
						} else if (this.formData.type == '银行卡') {
							this.edit_second_index(3)
							this.edit_second_value('银行卡')
						} else if (this.formData.type == '其它') {
							this.edit_second_index(4)
							this.edit_second_value('其它物品')
						}
					} else {
						// 此为found的其他物品
						this.edit_first_index(1)
						this.edit_second_index(5)
						this.edit_second_value('其它')
					}
					resolve()
				})
			},
			// 判断类型并传值
			insert_info() {
				switch (this.second_category_index) {
					// 校园卡类型
					case 0:
						this.$refs.myForm.formData.student_name = this.formData.student_name
						this.$refs.myForm.formData.student_ID = this.formData.student_ID
						break
						// 身份证类型
					case 1:
						this.$refs.myForm.formData.student_name = this.formData.student_name
						this.$refs.myForm.formData.ID_card_No = this.formData.ID_card_No
						break
						// 电影卡类型
					case 2:
						this.$refs.myForm.formData.movie_card_No = this.formData.movie_card_No
						break
						// 银行卡类型
					case 3:
						this.$refs.myForm.formData.bank_card_name = this.formData.bank_card_name
						this.$refs.myForm.formData.bank_card_No = this.formData.bank_card_No
						break
						// 其他物品类型
					default:
						this.$refs.myForm.formData.item_name = this.formData.item_name
						this.$refs.myForm.formData.description = this.formData.description
						break
				}
				this.$refs.myForm.formData.location = this.formData.location
				this.$refs.myForm.formData.contact_way = this.formData.contact_way
				uni.hideLoading()
			}
		},
		onLoad(options) {
			uni.showLoading({
				title: '加载中...'
			})
			this.formData = JSON.parse(options.data)
			this.$refs.myForm.update_id = this.formData._id
			this.estimate_type().then(() => {
				this.insert_info()
			})
		},
		// 当页面退出的时候执行
		onUnload() {
			this.edit_first_index(0)
			this.edit_second_index(0)
			this.edit_second_value('校园卡')
		}
	}
</script>

<style scoped lang="scss">
	.edit_box {
		width: 100vw;
		display: flex;
		flex-direction: column;
		align-items: center;
		
		/deep/ .van-notice-bar{
			width: 600rpx;
			position: relative;
		}

		.form {
			margin-top: -90rpx;
		}
	}
</style>
